package com.sky.controller.merchant;

import com.sky.dto.MerchantChangePasswordDTO;
import com.sky.dto.MerchantDTO;
import com.sky.dto.MerchantLoginDTO;
import com.sky.result.Result;
import com.sky.service.MerchantService;
import com.sky.vo.MerchantAvatarVO;
import com.sky.vo.MerchantLoginVO;
import com.sky.vo.MerchantRevenueVO;
import com.sky.vo.MerchantVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商家管理控制器
 */
@RestController
@RequestMapping("/merchant")
@Api(tags = "商家管理接口")
public class MerchantController {
    private static final Logger log = LoggerFactory.getLogger(MerchantController.class);

    @Autowired
    private MerchantService merchantService;

    /**
     * 商家登录
     * @param merchantLoginDTO 登录信息
     * @return 登录结果
     */
    @PostMapping("/login")
    @ApiOperation("商家登录")
    public Result<MerchantLoginVO> login(@RequestBody MerchantLoginDTO merchantLoginDTO) {
        log.info("商家登录请求，参数：{}", merchantLoginDTO);
        MerchantLoginVO loginVO = merchantService.login(merchantLoginDTO);
        log.info("商家登录成功，结果：{}", loginVO);
        return Result.success(loginVO);
    }

    /**
     * 编辑商家信息
     * @param merchantId 商家ID
     * @param merchantDTO 商家信息
     * @return 更新后的商家信息
     */
    @PutMapping("/{merchantId}/info")
    @ApiOperation("编辑商家信息")
    public Result<MerchantVO> updateInfo(@PathVariable Long merchantId, @RequestBody MerchantDTO merchantDTO) {
        log.info("编辑商家信息，商家ID：{}，参数：{}", merchantId, merchantDTO);
        MerchantVO merchantVO = merchantService.updateSelfInfo(merchantId, merchantDTO);
        log.info("编辑商家信息成功，结果：{}", merchantVO);
        return Result.success(merchantVO);
    }

    /**
     * 修改密码
     * @param merchantId 商家ID
     * @param passwordDTO 密码信息
     * @return 操作结果
     */
    @PutMapping("/{merchantId}/password")
    @ApiOperation("修改密码")
    public Result changePassword(@PathVariable Long merchantId, @RequestBody MerchantChangePasswordDTO passwordDTO) {
        log.info("修改密码请求，商家ID：{}，参数：{}", merchantId, passwordDTO);
        merchantService.changePassword(merchantId, passwordDTO);
        log.info("修改密码成功，商家ID：{}", merchantId);
        return Result.success();
    }

    /**
     * 上传商家头像
     * @param merchantId 商家ID
     * @param file 头像文件
     * @return 头像URL
     */
    @PostMapping("/{merchantId}/avatar")
    @ApiOperation("上传商家头像")
    public Result<MerchantAvatarVO> uploadAvatar(@PathVariable Long merchantId, @RequestParam("file") MultipartFile file) {
        log.info("上传商家头像，商家ID：{}，文件名：{}，大小：{}", merchantId, file.getOriginalFilename(), file.getSize());
        MerchantAvatarVO avatarVO = merchantService.uploadAvatar(merchantId, file);
        log.info("上传商家头像成功，结果：{}", avatarVO);
        return Result.success(avatarVO);
    }

    /**
     * 暂停营业
     * @param merchantId 商家ID
     * @return 商家信息
     */
    @PostMapping("/{merchantId}/business/pause")
    @ApiOperation("暂停营业")
    public Result<MerchantVO> pauseBusiness(@PathVariable Long merchantId) {
        log.info("暂停营业请求，商家ID：{}", merchantId);
        MerchantVO merchantVO = merchantService.pauseBusiness(merchantId);
        log.info("暂停营业成功，结果：{}", merchantVO);
        return Result.success(merchantVO);
    }

    /**
     * 开始营业
     * @param merchantId 商家ID
     * @return 商家信息
     */
    @PostMapping("/{merchantId}/business/resume")
    @ApiOperation("开始营业")
    public Result<MerchantVO> resumeBusiness(@PathVariable Long merchantId) {
        log.info("开始营业请求，商家ID：{}", merchantId);
        MerchantVO merchantVO = merchantService.resumeBusiness(merchantId);
        log.info("开始营业成功，结果：{}", merchantVO);
        return Result.success(merchantVO);
    }

    /**
     * 查看营业额
     * @param merchantId 商家ID
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param type 统计类型（daily/weekly/monthly）
     * @return 营业额统计结果
     */
    @GetMapping("/{merchantId}/revenue")
    @ApiOperation("查看营业额")
    public Result<MerchantRevenueVO> getRevenue(
            @PathVariable Long merchantId,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) String type) {
        log.info("查看营业额请求，商家ID：{}，开始日期：{}，结束日期：{}，统计类型：{}",
                merchantId, startDate, endDate, type);
        MerchantRevenueVO revenueVO = merchantService.getRevenue(merchantId, startDate, endDate, type);
        log.info("查看营业额成功，结果：{}", revenueVO);
        return Result.success(revenueVO);
    }

    /**
     * 商家退出
     * @param merchantId 商家ID
     * @return 操作结果
     */
    @PostMapping("/{merchantId}/logout")
    @ApiOperation("商家退出")
    public Result logout(@PathVariable Long merchantId) {
        log.info("商家退出请求，商家ID：{}", merchantId);
        merchantService.logout(merchantId);
        log.info("商家退出成功，商家ID：{}", merchantId);
        return Result.success();
    }

    /**
     * 获取当前商家信息
     * @param merchantId 商家ID
     * @return 商家信息
     */
    @GetMapping("/{merchantId}/info")
    @ApiOperation("获取当前商家信息")
    public Result<MerchantVO> getCurrentMerchant(@PathVariable Long merchantId) {
        log.info("获取当前商家信息请求，商家ID：{}", merchantId);
        MerchantVO merchantVO = merchantService.getCurrentMerchant(merchantId);
        log.info("获取当前商家信息成功，结果：{}", merchantVO);
        return Result.success(merchantVO);
    }
}
